package com.huowei.htzy.util;

import android.content.Context;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.Map;

import static android.content.ContentValues.TAG;

/**
 * Created by huowei on 2016/10/10.
 */

public class RequestUtil {

    private Context context;
    private RequestQueue requestQueue;
    private  RequestInterface anInterface;
    public RequestUtil(Context context, RequestInterface anInterface) {
        this.context = context;
        this.anInterface = anInterface;
        requestQueue =  Volley.newRequestQueue(context);
    }

    public void getRequest(String url){

        StringRequest stringRequest = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        anInterface.onSuccess(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                anInterface.onError(error);
            }
        });
        requestQueue.add(stringRequest);
    }
    public void  postRequest(String url, final Map<String, String> map){

        StringRequest stringRequest = new StringRequest(Request.Method.POST,url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        anInterface.onSuccess(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                anInterface.onError(error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
//                //在这里设置需要post的参数
//                Map<String, String> map = new HashMap<>();
//                map.put("name1", "value1");
//                map.put("name2", "value2");

                return map;
            }
        };

        requestQueue.add(stringRequest);

    }

}
